package com.company;
import java.util.Scanner;
public class Main {
    private static int PER_WIN = 0;//用户获胜的局数
    private static int COM_WIN = 0;//电脑获胜的局数
    private static int SUM = 0;//总局数
    private static char a;
    private static int sum = 0;//总轮数
    private static int win = 0;//用户获胜的轮数
    //定义两个数组分别存储用户名和密码
    static String[] arr1 = new String[3];
    static String[] arr2 = new String[3];
    private static String username;
    //定义一个输入类的对象
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        arr1[0] = "张三";
        arr2[0] = "123";
        arr1[1] = "李四";
        arr2[1] = "456";


        System.out.println("*************************");
        System.out.println("欢迎来到猜拳世界！");

        System.out.println("1.登录功能");
        System.out.println("2.注册新用户");
        System.out.println("3.退出游戏");
        System.out.println("请输入您选择的功能模块序号：");
        int id1 = sc.nextInt();
        switch (id1) {
            case 1:
                login();
                break;
            case 2:
                registe();
                System.out.println("请重新登录！");
                login();
                break;
            case 3:
                System.out.println("游戏结束");
                System.exit(0); //Java虚拟机以正常方式退出

            default:
                System.out.println("您输入的信息有误");
                System.out.println("#########游戏结束#########");
                System.exit(0);
                break;
        }

        System.out.println("您是否需要观看游戏规则？");
        System.out.println("1.需要。");
        System.out.println("2.不需要。");
        System.out.println("请输入您选择的序号：");
        int id2;
        do {
            id2 = sc.nextInt();
            if (id2 == 1 || id2 == 2) {
                switch (id2) {
                    case 1:
                        rule();
                        game();
                        break;
                    case 2:
                        game();
                        break;

                }
            } else {
                System.out.println("您的输入有误，请核对后重新输入！");
            }
        } while (id2 != 1 && id2 != 2);


    }

    //新用户注册功能
    private static void registe() {
        String username = null;
        while (true) {
            System.out.println("请输入用户名：");
            username = sc.next();

            //判断输入的用户名是否在数组中已经存在
            boolean flag = false;
            for (int i = 0; i < arr1.length; i++) {
                if (arr1[i] != null) {
                    if (arr1[i].equals(username)) {
                        flag = true;
                        break;
                    }
                }
            }
            if (flag) {
                System.out.println("您所输入的用户名已经存在，请重新输入。");
            } else {
                break;
            }
        }

        System.out.println("请输入密码：");
        String password = sc.next();
        //判断当前数组是否已经满了
        boolean flag = false;
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != null) {
                flag = true;
                break;
            }
        }

        if (flag == false) {
            //数组没有装满,
            for (int i = 0; i < arr1.length; i++) {
                if (arr1[i] == null) {
                    arr1[i] = username;
                    arr2[i] = password;
                    break;
                }
            }

        } else {
            //数组已经装满，对数组进行扩容
            String[] newArr1 = new String[arr1.length + 3];
            String[] newArr2 = new String[arr1.length + 3];

            //将原数组内的内容复制到新的数组里，可以通过for循环来实现，也可以通过arraycopy实现
            for (int i = 0; i < arr1.length; i++) {
                newArr1[i] = arr1[i];
                newArr2[i] = arr2[i];
            }
            System.arraycopy(arr1, 0, newArr1, 0, arr1.length);
            System.arraycopy(arr2, 0, newArr2, 0, arr2.length);

            arr1 = newArr1;
            arr2 = newArr2;
            //将用户名和密码存入数组
            for (int i = 0; i < arr1.length; i++) {
                if (arr1[i] == null) {
                    arr1[i] = username;
                    arr2[i] = password;
                    break;
                }
            }
        }
        System.out.println("注册成功");
    }

    //登录功能
    private static void login() {
        int i = 0;
        String password;
        do {
            System.out.println("请输入用户名：");
            username = sc.next();
            System.out.println("请输入密码：");
            password = sc.next();
            //判断输入用户名和密码是否在数组中存在
            for (i = 0; i < arr1.length; i++) {
                if (arr1[i] != null) {
                    if (arr1[i].equals(username)) {
                        if (arr2[i].equals(password)) {
                            System.out.println(username + "您好！您已经入系统！");
                            System.out.println("游戏开始！");
                            System.out.println("您是否需要观看游戏规则？");
                            System.out.println("1.需要。");
                            System.out.println("2.不需要。");
                            System.out.println("请输入您选择的序号：");
                            int id2;
                            do {
                                id2 = sc.nextInt();
                                if (id2 == 1 || id2 == 2) {
                                    switch (id2) {
                                        case 1:
                                            rule();
                                            game();
                                            break;
                                        case 2:
                                            game();
                                            break;

                                    }
                                } else {
                                    System.out.println("您的输入有误，请核对后重新输入！");
                                }
                            } while (id2 != 1 && id2 != 2);
                            System.exit(0);
                            break;
                        } else {
                            System.out.println("您输入的用户名或密码错误,请核对后重新输入！");
                            break;
                        }
                    } else {
                        continue;
                    }
                } else {
                    System.out.println("您输入的用户名不存在！是否进行注册？(y/n)");
                    char m = sc.next().charAt(0);
                    if (m == 'n') {
                        System.out.println("#########游戏结束#########");
                        System.out.println("######欢迎下次光临哦！######");
                        System.exit(0);
                    }
                    else if(m=='y'){
                        System.out.println("开始注册！");
                        registe();
                        System.out.println("请重新登陆！");
                        login();
                        System.out.println("游戏开始！");
                        System.out.println("您是否需要观看游戏规则？");
                        System.out.println("1.需要。");
                        System.out.println("2.不需要。");
                        System.out.println("请输入您选择的序号：");
                        int id2;
                        do {
                            id2 = sc.nextInt();
                            if (id2 == 1 || id2 == 2) {
                                switch (id2) {
                                    case 1:
                                        rule();
                                        game();
                                        break;
                                    case 2:
                                        game();
                                        break;

                                }
                            } else {
                                System.out.println("您的输入有误，请核对后重新输入！");
                            }
                        } while (id2 != 1 && id2 != 2);
                        System.exit(0);
                        break;
                    }
                    else{
                        System.out.println("您的输入有误！");
                        System.out.println("#########游戏结束#########");
                        System.out.println("######欢迎下次光临哦！######");
                        System.exit(0);
                    }
                    break;
                }
            }
        } while (username != arr1[i] || password != arr2[i]);

    }

    //游戏规则
    private static void rule() {
        System.out.println("------------------------------------------------------");
        System.out.println("游戏规则如下：");
        System.out.println("游戏中1代表石头，2代表剪刀，3代表布");
        System.out.println("此赛制为五局三胜制，您可以任意选择电脑玩家与您进行pk");
        System.out.println("------------------------------------------------------");
    }

    //游戏功能
    private static void game() {

        String per = "用户";
        String com = "电脑";

        String com1 = "时梦寒";
        String com2 = "户梦阁";
        String com3 = "王颍卓";
        String com4 = "付皓霜";


        System.out.println("请您从给出的选手中挑选一位与您进行pk：");
        System.out.println("1.时梦寒；");
        System.out.println("2.户梦阁；");
        System.out.println("3.王颍卓；");
        System.out.println("4.付皓霜；");
        System.out.println("请输入您选择的选手序号：");
        int id3 = sc.nextInt();
        switch (id3) {
            case 1:
                System.out.println(username + " pk " + com1);
                break;
            case 2:
                System.out.println(username + " pk " + com2);
                break;
            case 3:
                System.out.println(username + " pk " + com3);
                break;
            case 4:
                System.out.println(username + " pk " + com4);
            default :
                System.out.println("您的输入有误，请核对后重新输入！");

        }

        while (true) {


            System.out.println("请输入您的选择(1/2/3):");
            int id4 = sc.nextInt();//获取用户输入
            int computer = (int) (Math.random() * 3 + 1);//电脑随机出结果
            //用户出结果
            switch (id4) {
                case 1:
                    per = "石头";
                    break;
                case 2:
                    per = "剪刀";
                    break;
                case 3:
                    per = "布";
                    break;
                default:
                    per = "叮咚，您的输入无效，请核对后重新输入！";
                    break;
            }
            //电脑出结果
            switch (computer) {
                case 1:
                    com = "石头";
                    break;
                case 2:
                    com = "剪刀";
                    break;
                case 3:
                    com = "布";
                    break;
            }
            //根据输入结果比较判断输赢
            if (id4 == 1 && computer == 3 || id4 == 2 && computer == 1 || id4 == 3 && computer == 2) {
                System.out.println("你出的是（" + per + "） 对方出的是（" + com + "）=====>>>【噢噢,这局你输了！再接再厉！】");
                COM_WIN++;
                SUM++;
                System.out.println("你赢了" + PER_WIN + "局,对方赢了" + COM_WIN + "局.");

            } else if (id4 == computer) {
                System.out.println("你出的是（" + per + "） 对方出的是（" + com + "）=====>>>【嘿嘿,这局平局！再来一次吧！】");
                System.out.println("你赢了" + PER_WIN + "局,对方赢了" + COM_WIN + "局.");
                SUM++;
            } else if (id4 == 1 && computer == 2 || id4 == 2 && computer == 3 || id4 == 3 && computer == 1) {
                System.out.println("你出的是（" + per + "） 对方出的是（" + com + "）=====>>>【哈哈!这局你赢了！真厉害！】");
                PER_WIN++;
                SUM++;
                System.out.println("你赢了" + PER_WIN + "局,对方赢了" + COM_WIN + "局。");
            } else {
                System.out.println("------------------------------------------------------------------------------");
                System.out.println("叮咚!你的输入无效,请重新判断并输入合理的结果:※※ 1是剪刀，2是石头，3是布 ※※");
                System.out.println("------------------------------------------------------------------------------");
            }
            if (SUM == 5) {
                System.out.println();
                System.out.println("###################################");
                System.out.println("你赢了" + PER_WIN + "局,对方赢了" + COM_WIN + "局。");
                if (COM_WIN > PER_WIN) {
                    if (id3 == 1) {
                        System.out.println("很遗憾,你输给了时梦寒! 再接再厉哦! ");
                        System.out.println("###################################");
                    } else if (id3 == 2) {
                        System.out.println("很遗憾,你输给了户梦阁! 再接再厉哦! ");
                        System.out.println("###################################");
                    } else if (id3 == 3) {
                        System.out.println("很遗憾,你输给了王颍卓! 再接再厉哦! ");
                        System.out.println("###################################");
                    } else if (id3 == 4) {
                        System.out.println("很遗憾,你输给了付皓霜! 再接再厉哦! ");
                        System.out.println("###################################");
                    }
                } else if (COM_WIN == PER_WIN) {
                    if (id3 == 1) {
                        System.out.println("哦哦！你和时梦寒打成了平手。 ");
                        System.out.println("###################################");
                    } else if (id3 == 2) {
                        System.out.println("哦哦！你和户梦阁打成了平手。 ");
                        System.out.println("###################################");
                    } else if (id3 == 3) {
                        System.out.println("哦哦！你和王颍卓打成了平手。 ");
                        System.out.println("###################################");
                    } else if (id3 == 4) {
                        System.out.println("哦哦！你和付皓霜打成了平手。 ");
                        System.out.println("###################################");
                    }
                } else {
                    win++;
                    System.out.println("恭喜！你赢了！");
                }
                System.out.println("是否要进行下次比赛？（y/n）");
                a = sc.next().charAt(0);
                if (a == 'n') {
                    sum++;
                    System.out.println("您一共进行了" + sum + "轮比赛，共计赢了" + win + "轮比赛。");
                    System.out.println("#########游戏结束#########");
                    System.out.println("######欢迎下次光临哦！######");
                    System.exit(0);
                    break;
                } else if (a == 'y') {
                    PER_WIN = 0;
                    COM_WIN = 0;
                    SUM = 0;
                    sum++;

                } else {
                    sum++;
                    System.out.println("哦哦，输入错误！");
                    System.out.println("您一共进行了" + sum + "轮比赛，共计赢了" + win + "轮比赛。");
                    System.out.println("#########游戏结束#########");
                    System.out.println("######欢迎下次光临哦！######");
                    System.exit(0);
                    break;
                }

            }
        }
    }

    //查看功能
    private static void show () {
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != null) {
                System.out.println("用户名：" + arr1[i] + "   密码：" + arr2[i]);
            }

        }
    }
	// write your code here
    }

